package com.sgb.knowledgeshare.view.fragment;

import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareUserListBinding;
import com.sgb.knowledgeshare.view.activity.PlayListActivity;
import com.sgb.knowledgeshare.view.adapter.ShareUserListAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareMinUserListModel;
import com.swgk.core.base.AppFragment;
import com.swgk.core.base.model.entity.VideoBean;
import com.swgk.core.view.entity.VideoListEntity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fq on 2020/9/22 0022
 * Description：个人中心列表信息
 */
public class ShareMinUserListFragment extends AppFragment {
    private static String SHARETYPE = "share_type";
    private static String USER_ID = "user_id";
    private FragmentShareUserListBinding binding;
    private int UserShareType;
    private String userid;
    List<VideoBean> list ;
    private ShareUserListAdapter shareuserlistadapter;
    private boolean isLoadMore;
    private int page;
    ShareMinUserListModel viewModel;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private int pages;

    public static ShareMinUserListFragment newInstance(String userId, int type) {
        ShareMinUserListFragment fragment = new ShareMinUserListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(SHARETYPE, type);
        bundle.putString(USER_ID, userId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_user_list, container, false);
        viewModel = new ViewModelProvider(this).get(ShareMinUserListModel.class);
        viewModel.setActivity(ShareMinUserListFragment.this);
        return binding.getRoot();
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getArguments() != null) {
            UserShareType = getArguments().getInt(SHARETYPE);
            userid = getArguments().getString(USER_ID);
        }
        initview();
    }

    private void initview() {
        if (shareuserlistadapter == null) {
            shareuserlistadapter = new ShareUserListAdapter(getActivity(), null);
        }
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(shareuserlistadapter);
        com.github.jdsjlzx.ItemDecoration.GridItemDecoration divider = new GridItemDecoration.Builder(getContext())
                .setHorizontal(R.dimen.dp10)
                .setVertical(R.dimen.dp10)
                .setColorResource(R.color.white)
                .build();
        binding.lrecyclerview.addItemDecoration(divider);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 3);
        binding.lrecyclerview.setLayoutManager(layoutManager);
        binding.lrecyclerview.setAdapter(mLRecyclerViewAdapter);
        binding.lrecyclerview.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                getdata();
            }
        });
        binding.lrecyclerview.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                isLoadMore = true;
                page ++;
                if (page<=pages){
                    viewModel.getVideoListData(page, userid, UserShareType + "");
                }else {
                    binding.lrecyclerview.setNoMore(true);
                }
            }
        });
//    条目的点击事件
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(getActivity(), PlayListActivity.class);
                intent.putExtra(PlayListActivity.VIDEOLISTBEAN,(Serializable) list);
                intent.putExtra(PlayListActivity.PSO,position);
                intent.putExtra(PlayListActivity.VIDEOTYPE,"fx_mine");
                intent.putExtra(PlayListActivity.PAGE,page);
                intent.putExtra(PlayListActivity.USERNO,userid+"");
                intent.putExtra(PlayListActivity.VIDEOTITLE,UserShareType+"");
                getActivity().startActivity(intent);
            }
        });
        getdata();
    }

    private void getdata() {
        if (!TextUtils.isEmpty(userid)){
            isLoadMore = false;
            page = 1;
            viewModel.getVideoListData(page, userid, UserShareType + "");
        }
    }

    //    获取数据列表
    public void setVideoListData(VideoListEntity data) {
        if (!isAdded()) return;
        List<VideoBean> records = data.getRecords();
        pages = data.getPages();
        if (records == null) {
            shareuserlistadapter.setDatas(new ArrayList<>());
            return;
        }
        binding.lrecyclerview.refreshComplete(records.size());
        if (isLoadMore) {
            if (records.size()>0){
                shareuserlistadapter.addDatas(records);
            }else {

                //MToast.showToast(getContext(),"没有视频啦!");
            }
        } else {
            if (records.size()>0){
                binding.lrecyclerview.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                shareuserlistadapter.setDatas(records);
            }else {
                binding.lrecyclerview.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
                shareuserlistadapter.setDatas(null);
            }
        }
        this.list =shareuserlistadapter.getDatas() ;
    }


}